﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using StWapBrowser.BLL;
using System.Text.RegularExpressions;
using System.Threading;

namespace StWapBrowser
{
    public partial class frmProxyList : Form
    {
        DataTable dt = new DataTable();
        DateTime oldTime, newTime;
        bool IsTest;
        WebProxy wp = new WebProxy();

        public frmProxyList()
        {
            InitializeComponent();
        }

        private void frmProxyList_Load(object sender, EventArgs e)
        {
        }

        private DataTable InitDataTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("IP");
            dt.Columns.Add("Port");
            dt.Columns.Add("Type");
            dt.Columns.Add("SupportQQ", typeof(int));
            dt.Columns.Add("Speed", typeof(int));
            dt.Columns.Add("Area");

            return dt;
        }

        private void tsmiTest_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.AppStarting;
            oldTime = DateTime.Now;

            IsTest = true;
            oldTime = DateTime.Now;
            wp.RefreshIESettings(GetProxyInfo());
            this.wbAdProxyList.Navigate("http://www.sooot.cn/stTools/Ip/index.asp");

            this.Cursor = Cursors.Default;
            DateTime newTime = DateTime.Now;
        }

        private string GetProxyInfo()
        {
            int rowIndex = this.dgvProxyList.CurrentRow.Index;

            return dt.Rows[rowIndex][0].ToString() + ":" + dt.Rows[rowIndex][1].ToString();
        }

        private void LoadData()
        {
            HtmlOp ho = new HtmlOp();
            string htmlResult;

            dt = InitDataTable();

            htmlResult = ho.GetHtml("http://www.1flat.com/proxy_list.js", Encoding.Default);
            string[] strs = htmlResult.Split('=');
            htmlResult = strs[strs.Length - 1 < 0 ? 0 : strs.Length - 1];
            htmlResult = htmlResult.Replace("[[", "[").Replace("]];", "").Replace("[", "").Replace("],", "@").Replace("'", "");
            strs = htmlResult.Split('@');

            for (int i = 0; i < strs.Length; i++)
            {
                string[] strProxy = strs[i].Split(',');
                if (strProxy[2] == "2")
                {
                    strProxy[2] = "匿名";
                }
                else if (strProxy[2] == "3")
                {
                    strProxy[2] = "https";
                }
                else
                {
                    strProxy[2] = "未知";
                }

                strProxy[1] = dt.Compute(strProxy[1], "").ToString();

                dt.Rows.Add(strProxy);
            }

            this.dgvProxyList.AutoGenerateColumns = false;
            this.dgvProxyList.DataSource = dt;
            this.tsslStatus.Text = "本代理服务器信息(HTTP代理)均经过验证，每10分钟自动更新一次刚通过验证的IP!";
        }

        private void tsmiReadData_Click(object sender, EventArgs e)
        {
            try
            {
                this.tsslStatus.Text = "数据加载中，请稍候...";
                this.Cursor = Cursors.AppStarting;
                LoadData();
                this.Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tsmiCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(GetProxyInfo());
        }

        private void dgvProxyList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
           if (e.Button == MouseButtons.Right && e.RowIndex > -1)
            {
                this.dgvProxyList.CurrentCell = this.dgvProxyList.Rows[e.RowIndex].Cells[e.ColumnIndex];
                this.cmsProxyMenu.Show(Control.MousePosition);
            }
        }

        private void tsmiExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void wbAdProxyList_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            if (IsTest)
            {
                this.tsslStatus.Text = this.wbAdProxyList.ReadyState.ToString() + "正在测试连接，请稍候...";
            }
        }

        private void wbAdProxyList_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (IsTest)
            {
                newTime = DateTime.Now;
                IsTest = false;
                if (this.wbAdProxyList.DocumentTitle.Contains("速特网"))
                {
                    this.tsslStatus.Text = "测试完成。用时" + ((newTime - oldTime).TotalSeconds.ToString()) + "秒。";
                }
                else
                {
                    this.tsslStatus.Text = "连接失败，该代理不可用！";
                    MessageBox.Show("连接失败，该代理不可用！");
                }
            }
        }
    }
}
